export const navTo = (url:string, success?: Function) => {
    uni.navigateTo({
        url,
		success: () => {
			if (success) {
				success();
			}
		}
    })
}

export const copy = (str: string) => {
	uni.setClipboardData({
		data: str,
		success:() => {
			uni.showToast({//提示
				title:'复制成功' 
			})
		}
	});
}

/* 获取字典值 */
export const getDict = (type: string, value: string) => {
	const res = uni.getStorageSync("dict");
	const json = JSON.parse(res);
	const list = json.filter((item: any) => item.dictType === type);
	return list.find((item: any) => item.value === value);
}

export const getDictValue = (type: string, value: string) => {
	const res = uni.getStorageSync("dict");
	const json = JSON.parse(res);
	const list = json.filter((item: any) => item.dictType === type);
	const data=list.find((item: any) => item.value === value+"");
	if(data){
		return data.label;
	}
	return "";
}

/* 获取字典列表 */
export const getDictList = (type: string) => {
	const res = uni.getStorageSync("dict");
	const json = JSON.parse(res);
	const list = json.filter((item: any) => item.dictType === type);
	return list;
}

// 时间戳格式化成 YYYY-MM-DD
export const dateFormate=(timestamp:Date)=>{
   const d = new Date(timestamp); 
    // 注意：若时间戳是秒级(10位)，需先*1000转换为毫秒级
    return [
      d.getFullYear(),
      (d.getMonth() + 1).toString().padStart(2, '0'), // 月份补零
      d.getDate().toString().padStart(2, '0')         // 日期补零
    ].join('-');
}

export const confirm=(message:string)=> {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title: '提示',
      content: message,
      success: function (res) {
        if (res.confirm) {
          resolve(true); // 用户点击了确认
        } else if (res.cancel) {
          reject(false); // 用户点击了取消
        }
      },
      fail: function (err) {
        reject(err); // 处理错误情况
      }
    });
  });
}

export const getUserInfoData = () => {
	const userInfo = uni.getStorageSync('user-info');
	if (userInfo) {
		return JSON.parse(userInfo);
	}
	return undefined
}
	

export const checkPermi = (value: string[]) => {
  if (value && value instanceof Array && value.length > 0) {
	const permissionString = uni.getStorageSync("permission");
		if (permissionString) {
		const json = JSON.parse(permissionString);
		const permissions = json.permissions;
			
		const permissionDatas = value
		const all_permission = "*:*:*"

		const hasPermission = permissions.some(permission => {
		  return all_permission === permission || permissionDatas.includes(permission)
		})

		if (!hasPermission) {
		  return false
		}
		return true
	  } else {
		console.error(`need roles! Like checkPermi="['system:user:add','system:user:edit']"`)
		return false
	  }
  }
}

/**
 * 角色权限校验
 * @param {Array} value 校验值
 * @returns {Boolean}
 */
export const checkRole = (value:string[]) => {
  if (value && value instanceof Array && value.length > 0) {
		const permissionString = uni.getStorageSync("permission");
		if (permissionString) {
		const json = JSON.parse(permissionString);
		const roles = json.roles;
		const permissionRoles = value
		const super_admin = "admin"
		const hasRole = roles.some(role => {
		  return super_admin === role || permissionRoles.includes(role)
		})

		if (!hasRole) {
		  return false
		}
		return true
	  } else {
		console.error(`need roles! Like checkRole="['admin','editor']"`)
		return false
	  }
  }
}
